package com.frxuexi.park2301.my

import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.SPUtils
import com.frxuexi.lib_base.entity.MyHistoryBean
import com.frxuexi.lib_base.mvvm.BaseActivity
import com.frxuexi.lib_base.net.Const
import com.frxuexi.park2301.databinding.ActivityMyApplyBinding
import dagger.hilt.android.AndroidEntryPoint

@Route(path = "/app/MyApplyActivity")
@AndroidEntryPoint
class MyApplyActivity : BaseActivity<ActivityMyApplyBinding,MyViewModel>() {
    val myApplyAdapter by lazy { MyApplyAdapter() }
    val taskList = mutableListOf<MyHistoryBean>()
    val parkList = mutableListOf<MyHistoryBean>()
    val visitList = mutableListOf<MyHistoryBean>()
    val repairList = mutableListOf<MyHistoryBean>()

    override fun initData() {
        vm.taskData.observe(this){

            it.forEach {
                taskList.add(MyHistoryBean("巡更申请",it.patrolStatus,it.taskName,it.patrolTime))
            }
            myApplyAdapter.addData(taskList)
            myApplyAdapter.notifyDataSetChanged()
        }
        vm.parkData.observe(this){
            it.filter { it.userId==SPUtils.getInstance().getInt(Const.UID) }.forEach {
                parkList.add(MyHistoryBean("车位申请",it.spaceStatus,"申请${it.areaName}车位",vm.getDateTime(System.currentTimeMillis())))
            }
            myApplyAdapter.addData(parkList)
            myApplyAdapter.notifyDataSetChanged()
        }
        vm.visitData.observe(this){
            it.forEach {
                visitList.add(MyHistoryBean("来访申请",it.visitStatus,"访客：${it.visitorName}",it.appointmentTime))
            }
            myApplyAdapter.addData(visitList)
            myApplyAdapter.notifyDataSetChanged()
        }
        vm.repairData.observe(this){
            it.forEach {
                repairList.add(MyHistoryBean("报修申请",it.processingStatus,"${it.faultDescription}维修（${it.repairLocation}）",it.repairTime))
            }
            myApplyAdapter.addData(repairList)
            myApplyAdapter.notifyDataSetChanged()
        }
    }

    override fun initView() {
        binding.headMy.headerAdd.visibility=View.GONE
        binding.headMy.headerTitle.setText("我的申请")
        binding.headMy.headerBack.setOnClickListener {
            finish()
        }
        binding.rvMy.apply {
            adapter=myApplyAdapter
            layoutManager=LinearLayoutManager(this@MyApplyActivity)
        }
        vm.getTasks(SPUtils.getInstance().getInt(Const.UID))
        vm.getParks()
        vm.getVisits()
        vm.getRepairs()

        binding.myParkCon.setOnClickListener {
            myApplyAdapter.data.clear()
            myApplyAdapter.addData(parkList)
            myApplyAdapter.notifyDataSetChanged()
        }

        binding.myRepairCon.setOnClickListener {
            myApplyAdapter.data.clear()
            myApplyAdapter.addData(repairList)
            myApplyAdapter.notifyDataSetChanged()
        }

        binding.myVisitCon.setOnClickListener {
            myApplyAdapter.data.clear()
            myApplyAdapter.addData(visitList)
            myApplyAdapter.notifyDataSetChanged()
        }

        binding.myPatrolCon.setOnClickListener {
            myApplyAdapter.data.clear()
            myApplyAdapter.addData(parkList)
            myApplyAdapter.notifyDataSetChanged()
        }
    }

}